var app = getApp();
var c = require('../common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    des: '课程非常棒，五星好评',
    hiddeninfo: true,
    starl: [1, 1, 1, 1, 1],
    starld: [1, 1, 1, 1, 1],
    classon: 1,
    _fix: 0,
    taokeinfo: null,
    taokereclass: null,
    overflow: 'hidden',
    height: '100rpx',
    shoufang: 1,
    overflowc: 'hidden',
    heightc: '100rpx',
    shoufangc: 1,
    taokelist: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var goods_id = options.goods_id ? options.goods_id : 644;
    this.setData({
      goods_id: goods_id,
    })
    var user_id = wx.getStorageSync('userInfo').user_id;
    var sqlinfo = 'w(goods_id=' + goods_id + ')f(a.video_url,a.goods_id,money,a.discount_money,a.cate_id,b.introduce,a.number,c.name,b.type,b.teacherintroduce)jb(class|b.class_id=a.class_id)jc(user|b.uid=c.user_id)l(1)df(teacherintroduce|暂无简介)';
    var sqllist = 'w(topic_id=' + goods_id + ')fs(introduce|20)';
    //套课信息
    c.ajax(
      app.dbs + 'goods', {
        sql: sqlinfo
      },
      res => {
        this.setData({
          taokeinfo: res.data,
          cate_id: res.data.cate_id
        })

        var len = res.data.teacherintroduce.length;
        var lenc = res.data.introduce.length;

        if (len > 70) {
          this.setData({
            zan: 1
          })
        }
        if (lenc > 70) {
          this.setData({
            zanc: 1
          })
        }
      }
    )

    //套课列表(大纲)
    // c.ajax(
    //   app.dbs + 'goods', {
    //     sql: sqllist
    //   },
    //   res => {
    //     this.setData({
    //       taokelist: res.data
    //     })
    //   }
    // )
    // 相关课程
    c.ajax(
      app.url + 'reclass', {
        goods_id: goods_id
      },
      res => {
        this.setData({
          taokereclass: res.data
        })


      }
    )
    //评论
    c.ajax(
      app.url + 'comment', {
        goods_id: goods_id,
        user_id: user_id
      },
      res => {
        this.setData({
          taokerecommen: res.data,
          pfstarnum: res.data.pfstarnum,
          pfstarnum_not: res.data.pfstarnum_not,
          list: res.data.data,
          subscribe: res.data.subscribe
        })
        console.log(res.data);
      }

    )

    //返回对应状态
    c.ajax(
      app.url + 'detailstatus', {
        goods_id: goods_id,
        user_id: user_id
      },
      res => {
        this.setData({
          status: res.data.status,
        })
      }

    )

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var query = wx.createSelectorQuery().in(this)
    var that = this;
    query.select('#topinfo').boundingClientRect(function (res) {
      that.setData({
        topmenu: res.top
      }) // 这个组件内 #topinfo 节点的上边界坐标
    }).exec()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  ckickon: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      classon: id
    });
  },
  onPageScroll: function (scroll, that) {
    var topmenu = this.data.topmenu;
    if (scroll.scrollTop > topmenu) {
      this.setData({
        _fix: 1
      });
    } else {
      this.setData({
        _fix: 0
      });
    }
  },
  zanoperation: function () {
    this.setData({
      shoufang: 2,
      overflow: '',
      height: 'auto',
    })
  },
  shouoperation: function () {
    this.setData({
      shoufang: 1,
      overflow: 'hidden',
      height: '100rpx',
    })
  },
  zanoperationc: function () {
    this.setData({
      shoufangc: 2,
      overflowc: '',
      heightc: 'auto',
    })
  },
  shouoperationc: function () {
    this.setData({
      shoufangc: 1,
      overflowc: 'hidden',
      heightc: '100rpx',
    })
  },
  // 收藏
  clicksub: function (e) {
    var goods_id = e.currentTarget.dataset.goods_id;

    var user_id = wx.getStorageSync('userInfo').user_id;
    c.ajax(
      app.url + 'addSubscript', {
        goods_id: goods_id,
        user_id: user_id
      },
      res => {
        this.setData({
          subscribe: res.data,
        })

      }
    )
  },
  staradd: function (e) {
    var starl = [];
    var starb = [];
    var num = e.currentTarget.dataset.num;
    console.log(num);
    for (var i = 0; i < num; i++) {
      starl[i] = 1;
    }
    for (var i = 0; i < (5 - num); i++) {
      starb[i] = 1;
    }

    this.setData({
      starl: starl,
      starb: starb
    });
  },
  stardee: function (e) {
    var starb = [];
    var num = e.currentTarget.dataset.num;
    var starl = this.data.starl;
    var lcount = starl.length;
    //在亮星的基础上网上添加
    for (var i = lcount; i <= lcount + num; i++) {
      starl[i] = 1;
    }
    var lcountdel = starl.length;
    for (var i = 0; i < (5 - lcountdel); i++) {
      starb[i] = 1;
    }
    this.setData({
      starl: starl,
      starb: starb
    });
  },
  stars: function () {
    this.setData({ hiddeninfo: false });
  },
  title: function (e) {
    this.data.title = e.detail.value;
  },
  sendpl: function () {
    var starl = this.data.starl;
    var title = this.data.title;
    var starnum = starl.length;
    var user_id = wx.getStorageSync('userInfo').user_id;
    var goods_id = this.data.goods_id;
    c.ajax(
      app.url + 'sendpl', {
        title: title,
        starnum: starnum,
        user_id: user_id,
        goods_id: goods_id
      },
      res => {
        console.log(res.data);
        if (res.data == 1) {
          this.setData({
            hiddeninfo: true,
          })
        }
        wx.showToast({
          title: '发表成功',
        })
    
        
      }
    )
  },
  deletes: function () {
    this.setData({
      hiddeninfo: true,
    })
  },
  //团购原价
  yuanprice: function () {
    var money = this.data.taokeinfo.money;
    var goods_id = this.data.goods_id;
      wx.navigateTo({
        url: '/pages/ordersure/ordersure?goods_id=' + goods_id  + '&money=' + money,
      })
  },
  //团购价钱
  tuanprice: function () {
    var money = this.data.taokeinfo.discount_money;
    var goods_id = this.data.goods_id;
    var tuan=1;
    wx.navigateTo({
      url: '/pages/ordersure/ordersure?goods_id=' + goods_id + '&money=' + money + '&tuan=' + tuan,
    })
  },
  //会员价钱
  vipprice: function () {
    var money = this.data.taokeinfo.money;
    var goods_id = this.data.goods_id;
    var vip = app.u.is_vip;
    if (vip != 1) {
      wx.showToast({
        title: '您还不是会员！',
        icon: 'none'
      })
    } else {
      wx.navigateTo({
        url: '/pages/ordersure/ordersure?goods_id=' + goods_id + '&vip=' + vip + '&money=' + money,
      })
    }
  },
  //会员原价
  vippriceyuan: function () {
    var money = this.data.taokeinfo.money;
    var goods_id = this.data.goods_id;
    wx.navigateTo({
      url: '/pages/ordersure/ordersure?goods_id=' + goods_id + '&money=' + money,
    })
  },
  shopclick: function () {
    var goods_id = this.data.goods_id;
    var money = this.data.taokeinfo.money;
    wx.navigateTo({
      url: '/pages/ordersure/ordersure?goods_id=' + goods_id + '&money=' + money,
    })
  },
  aasaa:function(){
    wx.createVideoContext(videoid, this);
    
  }
})